package com.csu.letaobackend.serviceM.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.PictureDao;
import com.csu.letaobackend.persistence.UserDao;
import com.csu.letaobackend.serviceM.UserMService;
import com.csu.letaobackend.vo.UserVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class UserMServiceImpl implements UserMService {

      @Autowired
      UserDao userDao;

      @Autowired
      PictureDao pictureDao;


      @Override
      public CommonResponse<IPage<UserVO>> getAllUsers(int pageid, int pagesize) {
            IPage<User> userIPage=new Page<>(pageid,pagesize);
            userDao.selectPage(userIPage,null);

            List<UserVO> userVOList=new ArrayList<>();
            for(User user:userIPage.getRecords()){
                  userVOList.add(entityToVO(user));
            }
            IPage<UserVO> userVOIPage=new Page<>(pageid,pagesize);
            userVOIPage.setTotal(userIPage.getTotal());
            userVOIPage.setRecords(userVOList);

            return CommonResponse.createForSuccess(userVOIPage);
      }

      @Override
      public CommonResponse<IPage<UserVO>> searchUser(String username, int pageid, int pagesize) {
            IPage<User> userIPage=new Page<>(pageid,pagesize);

            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("username",username);
            userDao.selectPage(userIPage,queryWrapper);

            List<UserVO> userVOList=new ArrayList<>();
            for(User user:userIPage.getRecords()){
                  userVOList.add(entityToVO(user));
            }
            IPage<UserVO> userVOIPage=new Page<>(pageid,pagesize);
            userVOIPage.setTotal(userIPage.getTotal());
            userVOIPage.setRecords(userVOList);

            return CommonResponse.createForSuccess(userVOIPage);
      }

      @Override
      public CommonResponse updateAvailable(String username, int available) {
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
            updateWrapper.eq("username",username).set("available",available);
            userDao.update(null,updateWrapper);
            return CommonResponse.createForSuccess("更新状态成功");
      }

      @Override
      public CommonResponse deleteUser(String username) {
            User user = userDao.selectById(username);

            if(user==null){
                  return  CommonResponse.createForError("用户不存在");
            }
            else{
                  userDao.deleteById(username);
            }
            return CommonResponse.createForSuccessMessage("删除成功");
      }

      private UserVO entityToVO(User user) {
            UserVO userVO = new UserVO();
            userVO.setUsername(user.getUsername());
            userVO.setPassword(user.getPassword());
            userVO.setPhone(user.getPhone());
            userVO.setPictureid(user.getPictureid());
            userVO.setSex(user.getSex());
            userVO.setAge(user.getAge());
            userVO.setEmail(user.getEmail());
            userVO.setCountry(user.getCountry());
            userVO.setSignature(user.getSignature());
            userVO.setAvailable(user.getAvailable());

            userVO.setPicturesrc(pictureDao.selectById(user.getPictureid()).getSrc());
            return userVO;
      }
}
